package com.he.hsdyc.controller;

import com.he.hsdyc.util.BigDecimalUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 〈〉
 *  对外接口
 * @author wangmx
 * @since 1.0.0
 */
@RestController
@RequestMapping("/api")
public class MemberController {

    @GetMapping("hello")
    @PreAuthorize("hasAnyAuthority('hello')")
    public String hello(){
        return "hello";
    }

    @GetMapping("current")
    public Principal user(Principal principal) {
        return principal;
    }

    @GetMapping("query")
    @PreAuthorize("hasAnyAuthority('query')")
    public String query() {
        return "have query permission";
    }


    @ApiOperation("时间格式测试")
    @RequestMapping(value = "testDate",method = RequestMethod.GET)
    public String testDate(@RequestParam String month) {
        //此种方式月份会自动加1，不推荐
        Date date1 = new Date(2017-1900,12,1,12,00,00);//Sun Jan 18 12:00:36 CST 1970
        Date date2 = new Date(2017-1900,11,18,12,00,00);
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sdate1 = sdf.format(date1);//2017-02-01 12:00:00
        String sdate2 = sdf.format(date2);//2017-12-18 12:00:00
        System.out.println("=================sdate1"+sdate1);
        System.out.println("=================sdate1"+sdate2);

        Date  date3 = new Date(1483236000);//2017/01/01 10:00:00    Sun Jan 18 12:00:36 CST 1970
        String sdate3 = sdf.format(date3);
        System.out.println("===================new Date(long)UNIX转北京时间："+sdate3);//1970-01-18 12:00:36,转的是秒而非毫秒，所以转换错误



        //自定义时间，推荐写法
        Date date4 = new Date(1496282400000l);//自定义时间，由于new Date(String s)已经废弃，所以用如下方法，转为UNIX时间戳，后面加L,
        //此时返回的是date默认格式，需要格式转换成我们需要的格式，即由：Sun Jan 18 12:00:36 CST 1970   转为：2017/01/01 10:00:00
        String sdate4 = sdf.format(date4);
        System.out.println("=============="+sdate4);//2017-06-01 10:00:00




      /*  String month_regex = "^(0?[1-9]|1[0-2])$";
        if(month.matches(month_regex))
            System.out.println("==================匹配1-12:success");
        else System.out.println("==================匹配1-12:failed");*/

        //测试正则，是否为正整数
        String regex = "^[1-9]\\d*$";
        if(month.matches(regex))
            System.out.println("==================匹配正整数:success");
        else System.out.println("==================匹配正整数:failed");


        //容易犯错的地方，必须用equals
        Integer a = 2000;
        Integer b= 2000;
        System.out.println("======================2000 == 2000的结果是："+(a == b));
        System.out.println("======================2000.equals(2000)的结果是："+(a.equals(b)));



        //当前时间到设定时间剩余的秒数，倒计时
        Date publishDate = new Date(1515114900000L);//2018/1/5 09:15:00
        Date currentDate = new Date();
        long exprieTime = (publishDate.getTime() - currentDate.getTime()) / 1000;
        System.out.println("====================publishDate:"+publishDate);
        System.out.println("====================currentDate:"+currentDate);
        System.out.println("====================exprieTime:"+exprieTime);


        System.out.println("========================Date"+publishDate);//Dec 26 14:50:00 CST 2017
        System.out.println("========================Date.getTime()"+publishDate.getTime());//1514271000000
        System.out.println("========================Date.format"+sdf.format(publishDate));//2017-12-26 14:50:00


        //运营时长
        Date establishDate =  new Date(1415667600000L);//2014/11/11 9:0:0
        Date curDate = new Date();
        List<String> operateTime = BigDecimalUtil.getDatePoor(establishDate, curDate);
        System.out.println("=========================运营时长:"+operateTime);


        //金额划分
        Double d = 150005555.66;
        List<String> typeConversion = BigDecimalUtil.typeConversion(d);
        System.out.println("=========================累计投资金额"+typeConversion);

        return  "success";
    }

}
